<?php
if (! defined ( 'SALT' )) {
	define ( 'SALT', 'MoneyBackReviewsAllDayLong313' );
}

require_once (dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . "aiconfig.php");

function curPageURL() {
	$pageURL = 'http';
	if ($_SERVER ["HTTPS"] == "on") {
		$pageURL .= "s";
	}
	$pageURL .= "://";
	if ($_SERVER ["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER ["SERVER_NAME"] . ":" . $_SERVER ["SERVER_PORT"] . $_SERVER ["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER ["SERVER_NAME"] . $_SERVER ["REQUEST_URI"];
	}
	return $pageURL;
}

if (! function_exists ( 'apache_request_headers' )) {
	function apache_request_headers() {
		$arh = array ();
		$rx_http = '/\AHTTP_/';
		foreach ( $_SERVER as $key => $val ) {
			if (preg_match ( $rx_http, $key )) {
				$arh_key = preg_replace ( $rx_http, '', $key );
				$rx_matches = array ();
				// do some nasty string manipulations to restore the original letter case
				// this should work in most cases
				$rx_matches = explode ( '_', $arh_key );
				if (count ( $rx_matches ) > 0 and strlen ( $arh_key ) > 2) {
					foreach ( $rx_matches as $ak_key => $ak_val )
						$rx_matches [$ak_key] = ucfirst ( $ak_val );
					$arh_key = implode ( '-', $rx_matches );
				}
				$arh [$arh_key] = $val;
			}
		}
		return ($arh);
	}

}

$domainTrackingList = explode ( ',', domains );
$headers = apache_request_headers ();
$currentHost = str_ireplace ( 'www.', '', $headers ['HOST'] );
if (strlen ( $currentHost ) == 0) {
	$currentHost = 'localhost';
}

$trackAd = false;
if (count ( $domainTrackingList ) == 0) {
	$trackAd = true;
} else {
	foreach ( $domainTrackingList as $domain )
		if (stripos ( $domain, $currentHost ) !== false) {
			$trackAd = true;
			break;
		}
}

// Check the current page location and do not filter if it's an wordpress admin page (joomla or any other blogging software needs to not filter their content.
if (stripos ( curPageURL (), "wp-admin" ) == false && $trackAd) {
	ob_start ( 'filterAffilatePage' );
}
function encrypt($text) {
	return trim ( base64_encode ( mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) ) ) );
}

function decrypt($text) {
	return trim ( mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, SALT, base64_decode ( $text ), MCRYPT_MODE_ECB, mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND ) ) );
}

function filterAffilatePage($input) {
	global $aicon, $currentHost;
	
	// Get account constants defined by .htaccess file or config file
	$gaUser = '';
	$gaPass = '';
	$gawUser = '';
	$gawPass = '';
	$cbUser = '';
	$cbPass = '';
	
	// Get Tracking ID
	require_once (dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . "includes" . DIRECTORY_SEPARATOR . "getaitrackingcode.php");
	
	$cookieName = $currentHost . 'aia' . aiaffiliateid;
	
	if (isset ( $_COOKIE [$cookieName] )) {
		// Get tracking value from cookie
		$tid = $_COOKIE [$cookieName];
	} else {
		// Set cookie
		setcookie ( $cookieName, $tid, null, "/", "." . $currentHost );
	}
	
	//  echo "Date of first visit: ".$aux->first_visit."<br />";
	//  echo "Date of previous visit: ".$aux->previous_visit."<br />";
	//  echo "Date of current visit: ".$aux->current_visit_started."<br />";
	//  echo "Times visited: ".$aux->times_visited."<br />";
	//  echo "Pages viewed current visit: ".$aux->pages_viewed."<br />";
	

	// Update all affiliate links in $input to reflect tracking code
	//$input = preg_replace ( "/([\"|'|\s].*hop\.clickbank\.net.*)(tid=[a-zA-Z0-9]+)(.*[\"|'|\s])/i", "$1tid=" . $tid . "$3", explode ( " ", $input ) );
	//$input = preg_replace ( "/(href=[\"|'].*hop\.clickbank\.net)([\"|'])/i", "$1?tid=" . $tid . "$2", $input );
	

	$input = preg_replace ( "/(href=.*hop\.clickbank\.net.*|url=.*hop\.clickbank\.net.*)(tid=[a-zA-Z0-9]+)(.*[\"|'])/i", "$1tid=" . $tid . "$3", explode ( " ", $input ) );
	$input = preg_replace ( "/(href=.*hop\.clickbank\.net[\/]?|url=.*hop\.clickbank\.net[\/]?)([\"|'])/i", "$1?tid=" . $tid . "$2", $input );
	
	// Put the spaces back into the $input
	$input = implode ( " ", $input );
	
	if (isset ( $_REQUEST ['debug'] )) {
		$input .= "Current Host: " . $currentHost . "<br>";
		$campaignData = "";
		foreach ( $trackingInfo as $key => $data ) {
			$campaignData .= $key . " => " . $data . "<br>";
		}
		$input .= "<br> page updated by AI-Affiliate. <br>Campaign Data (tid=" . $tid . "):<br>" . $campaignData;
	}
	
	return $input;
}
?>